package com.aidong.carnival.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.aidong.carnival.controllers.concerns.Failure;
import com.aidong.carnival.controllers.concerns.ListType;
import com.aidong.carnival.controllers.concerns.Result;
import com.aidong.carnival.controllers.concerns.Success;
import com.aidong.carnival.controllers.concerns.View.WithPhoto;
import com.aidong.carnival.controllers.concerns.View.WithPhotoList;
import com.aidong.carnival.entity.Talent;
import com.aidong.carnival.entity.Vote;
import com.aidong.carnival.service.TalentService;
import com.fasterxml.jackson.annotation.JsonView;

@RestController
@RequestMapping("/carnival")
public class TalentsController {

	@Autowired
	private TalentService talentService;

	@JsonView(WithPhoto.class)
	@RequestMapping(value = "/talents", method = RequestMethod.GET, produces = "application/json")
	@ResponseBody
	public Result list(@RequestParam(value = "carnivalId", defaultValue = "1") int carnivalId,
			@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "list", defaultValue = "home") String listType) {
		System.err.println("请求参数:" + page + ":" + listType);
		ModelMap map = new ModelMap();
		switch (ListType.fromString(listType)) {
		case home:
			map.addAttribute("member", talentService.list(carnivalId, ListType.member.toString(), page));
			map.addAttribute("coach", talentService.list(carnivalId, ListType.coach.toString(), page));
			break;
		case member:
			map.addAttribute("talent", talentService.list(carnivalId, listType, page));
			break;
		case coach:
			map.addAttribute("talent", talentService.list(carnivalId, listType, page));
			break;
		default:
			break;
		}
		return new Success(map);
	}

	@JsonView(WithPhotoList.class)
	@RequestMapping(value = "/talents/{idongId}", method = RequestMethod.GET, produces = "application/json")
	@ResponseBody
	public Result detail(@PathVariable("idongId") int idongId,
			@RequestParam(value = "carnivalId", defaultValue = "1") int carnivalId) {
		Talent talent = talentService.findByIdongId(carnivalId, idongId);
		if (talent == null) {
			return new Failure("查看的用户还未报名");
		} else {
			return new Success(talent);
		}
	}

	@JsonView(WithPhotoList.class)
	@RequestMapping(value = "/talents/{idongId}", method = RequestMethod.POST, produces = "application/json")
	@ResponseBody
	public Result vote(@PathVariable("idongId") int votedId,
			@RequestParam(value = "idongId", required = true) int voterId,
			@RequestParam(value = "carnivalId", defaultValue = "1") int carnivalId) {
		Talent talent = talentService.findByIdongId(carnivalId, votedId);
		if (talent == null) {
			return new Failure("您投票的用户还未参加达人报名");
		} else {
			int votedCount = talentService.votedCount(carnivalId, voterId);
			if (votedCount >= 3) {
				return new Failure("您当日票数已用完");
			} else {
				Vote vote = new Vote(carnivalId, votedId, voterId);
				talentService.vote(vote);
				return new Success(new ModelMap());
			}
		}
	}
}
